package Controlor.Action;

import Controlor.Server.UserServer;
import Controlor.ServerImpl.UserServerImpl;
import Model.Entity.User;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/UserAction/*")
public class UserAction extends BaseAction {
    private UserServer userServer;
    public UserAction(){
        userServer = new UserServerImpl();
    }
    //登录
    public void login(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        User user = new User();
        user.setName(req.getParameter("name"));
        user.setPassword(req.getParameter("pwd"));
        int rst = userServer.login(user);
        if(rst==1){
            HttpSession session=req.getSession();
            session.setAttribute("username", user.getName());
            //转到系统主界面

            resp.sendRedirect("/index.jsp");
        }
        else {
            PrintWriter pw = resp.getWriter();
            pw.write("<script type='text/javascript'>alert('账号密码错误!');document.location.href=\"/index.jsp\";</script>");
            pw.flush();
            pw.close();
        }
    }

}

